package com.qd.videorecorder;

import java.io.File;
import java.io.FileFilter;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.qd.videorecorder.adapter.RecordLocalAdapter;
import com.qd.videorecorder.util.RecordMixMp3;

public class RecordLocalMusic extends FinalActivity {
	
	static {
		System.loadLibrary("mad");  
	}  
	
	@ViewInject(id=R.id.main_btn_below , click="viewClick") Button main_btn_below;
	@ViewInject(id=R.id.topbar_title ) TextView topbar_title;
	@ViewInject(id=R.id.btn_myring , click="viewClick") Button btn_myring;
	@ViewInject(id=R.id.lv_local_mp3) ListView lv_local_mp3;
	@ViewInject(id=R.id.ll_alert) LinearLayout ll_alert;
	@ViewInject(id=R.id.tv_alert) TextView tv_alert;
	
	private boolean refreash = false;
	
	private RecordLocalAdapter recordLocalAdapter;
	
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.singandmusic);
		
		main_btn_below.setBackgroundResource(R.drawable.icon_return);
		btn_myring.setVisibility(View.GONE);
		
		String title = getIntent().getStringExtra("title");
		
		if(title==null){
			title = getResources().getString(R.string.app_name);
		}
		
		topbar_title.setText(title);
		
		recordLocalAdapter = new RecordLocalAdapter(this);// 创建适配器
		
		lv_local_mp3.setOnItemLongClickListener(new OnItemLongClickListener()
 		{

 			@Override
 			public boolean onItemLongClick(AdapterView<?> parent, View view,
 					final int position, long id)
 			{
 				// TODO Auto-generated method stub
 				
 				new AlertDialog.Builder(RecordLocalMusic.this)
                 .setTitle("提示")
                 .setMessage("删除该文件？")
                 .setPositiveButton("是", new DialogInterface.OnClickListener()
 				{
 					@Override
 					public void onClick(DialogInterface dialog, int which)
 					{
 						// TODO Auto-generated method stub
 						dialog.dismiss();
 						try {
 							
     						File delFile = (File) recordLocalAdapter.getItem(position);
     						
     						if(delFile.exists())
     						{
     							if(delFile.delete())
     								intFiles();
     						}
     						
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
 					}
 				})
                 .setNegativeButton("否", new DialogInterface.OnClickListener()
 				{
 					
 					@Override
 					public void onClick(DialogInterface dialog, int which)
 					{
 						// TODO Auto-generated method stub
 						dialog.cancel();
 					}
 				})
                 .create().show();
 				
 				return true;
 			}
 		});
		
		intFiles();
		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		recordLocalAdapter.stopPlayer();
	}



	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		intFiles();
	}

	public void uploadFile(String filename,String filepath){
		// 上传本地铃声
//		Intent intent=new Intent(this,ycrecorderupload_myrec.class);
//	    intent.putExtra("title","上传");
//	    intent.putExtra("cl_name", filename);
//	    intent.putExtra("cl_path", filepath);
//	    intent.putExtra("cutSong", true);
//	    startActivityForResult(intent, 0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			if(requestCode==0){
				refreash = true;
			}
		}
	}

	private void intFiles(){
		String path = RecordMixMp3.getLocalPath() + "/music/record/";
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
			lv_local_mp3.setVisibility(View.GONE);
			ll_alert.setVisibility(View.VISIBLE);
			tv_alert.setText("您还没有录制");
		}else{
			ll_alert.setVisibility(View.GONE);
			
			FileFilter fileFilter = new FileFilter() {
				
				@Override
				public boolean accept(File pathname) {
					// TODO Auto-generated method stub
					if(pathname.getName().endsWith(".mp3")){
						return true;
					}
					return false;
				}
			};
			
			File[] files = file.listFiles(fileFilter);
			if(files.length>0){
				lv_local_mp3.setVisibility(View.VISIBLE);
				recordLocalAdapter.setData(files);
				if(lv_local_mp3.getAdapter()==null){
					lv_local_mp3.setAdapter(recordLocalAdapter);
				}
				recordLocalAdapter.notifyDataSetChanged();
			}else{
				lv_local_mp3.setVisibility(View.GONE);
				ll_alert.setVisibility(View.VISIBLE);
				tv_alert.setText("您还没有录制");
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if(keyCode==KeyEvent.KEYCODE_BACK){
			Intent data = new Intent();
			data.putExtra("refreash", refreash);
			setResult(RESULT_OK, data);
			finish();
			return false;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	public native int decodeMp3ToPCM2(String mp3File, String mp3PCM);
	
	public void viewClick(View v){
		switch (v.getId())
			{
			case R.id.main_btn_below:
				Intent data = new Intent();
				data.putExtra("refreash", refreash);
				setResult(RESULT_OK, data);
				finish();
				break;

			default:
				break;
			}
	}
}
